<?php
App::import('Model','Assets.Asset');
class ListingAsset extends Asset 
{
	var $name = 'ListingAsset';
	
	const PUBLISHED = 1;
	const UNPUBLISHED = 0;
	var $scopes = array(
		'published' => array('ListingAsset.status'=>self::PUBLISHED)
	);
	var $belongsTo = array(
						'User'=> array(
								'className' => 'Users.User',
								'foreignKey' => 'user_id',
								'dependent' => true,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'counterCache'=>true,
								'counterScope'=>array('ListingAsset.status'=>self::PUBLISHED)
							),

						'Listing'=>array(
								'className' => 'Listing',
								'foreignKey' => 'listing_id',
								'dependent' => true,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'counterCache'=>true,
								'counterScope'=>array('ListingAsset.status'=>self::PUBLISHED)
							)
					);	

	var $actsAs = array('Comments.Commentable'=>array(
								'commentModel'=>'AssetComment',
								'dependent'=>true
								),
						'Users.UserContent'=>array(
									'posterModels'=>array('Users.User'=>array('dependent'=>true),'Users.Guest')
								),
						'Containable',
            'AutoModerator.AutoModerator'=> array() 
					);
					
	function afterInitialize()
	{
		$isImageRequired = cr('ListingConfig.listing_image_required');
		if($isImageRequired)
		{
			$empty = false;
		}else
		{
			$empty = true;
		}		
		$uploadConfig = array("name"=> array('dir'=>WWW_ROOT.'uploads'.DS.'listing_assets'.DS,'mediaTypes'=>array("Image"),'empty'=>$empty));				
		$this->Behaviors->attach('Upload',$uploadConfig);
		$this->additionalFields[] = "file_url";
		parent::afterInitialize();		
	}
	
	function beforeSave()
	{		
		$return = parent::beforeSave();
		if(!isset($this->data['ListingAsset']['name']) && count($this->data['ListingAsset']) > 2)
		{
			unset($this->data['ListingAsset']);
		}
		return $return;				
	}
	
	function file_url($asset)
	{
		if(isset($asset[$this->alias]['name']))
		{
			return '/uploads/listing_assets/'.$asset[$this->alias]['name'];
		}
		return false;
	}
	
	function beforeChildrenSave($data,$ListingModel)
	{
		$userId = null;
		if(isset($ListingModel->saveAllData['Listing']['user_id']))
		{
			$userId = $ListingModel->saveAllData['Listing']['user_id'];
		}else if(isset($ListingModel->saveAllData['Listing']['id']))
		{
			$ownerId = $ListingModel->field("owner_id",array("id"=>$ListingModel->saveAllData['Listing']['id']));
			if(!$ownerId)
			{
				$userId = $ListingModel->field("user_id",array("id"=>$ListingModel->saveAllData['Listing']['id']));
			}else
			{
				$userId = $ownerId;
			}
		}
		foreach($data as $key => $attachment)
		{
			if(!isset($attachment['name']) || !$attachment['name']['name'])
			{
					unset($data[$key]);
			}else
				{
					$data[$key]['status'] = self::PUBLISHED;
					if($userId)
					{
						$data[$key]['user_id'] = $userId;
					}
				}
		}
		return $data;
	}
	
	/***
	 * set $listingAssetId to be default for listingId
	 */
	function makeAsDefault($listingId,$listingAssetId)
	{
		$listingAssetId = $this->field($this->alias.".id",array($this->alias.".id"=>$listingAssetId,$this->alias.".listing_id"=>$listingId));
		if($listingAssetId)
		{
			$this->updateAll(array('is_default'=>0),array('ListingAsset.listing_id'=>$listingId));
			$this->id = $listingAssetId;
			$this->saveField('is_default',1);
			$this->Listing->clearRecordCache($listingId,true);
			return true;
		}
		return false;
	}
				
}
?>